var PLANE_DEVICE = window.NameSpace || {};

var DP_PLANES_MAP = new Map();

PLANE_DEVICE.loadBiInfoBtn = function(flag) {
	if (flag == 0) {
		// 当日
		if ($('#mp_today_count').prop("checked") == true) {
			$('#mp_total_count').prop("checked", false);
			PLANE_DEVICE.loadBiInfo(flag);
		}
	} else {
		// 累计
		if ($('#mp_total_count').prop("checked") == true) {
			$('#mp_today_count').prop("checked", false);
			PLANE_DEVICE.loadBiInfo(flag);
		}
	}

}

PLANE_DEVICE.loadDevicesInfo = function() {
	$
			.ajax({
				url : "/device/loadDevicesInfo",
				async : true,
				success : function(data) {
					if (KUFLAG) {
						$('#kuDevicesNum').text(
								formatNum(data.count.kuDevicesNum, 0, 1));
						$('#kuDevicesWarn').text(
								formatNum(data.warn.kuDevicesWarn, 0, 1));
					}
					// server
					$('#wifiDevicesNum').text(
							formatNum(data.count.wifiDevicesNum, 0, 1));
					$('#wifiDevicesWarn').text(
							formatNum(data.warn.wifiDevicesWarn, 0, 1));
					// cap
					$('#capDevicesNum').text(
							formatNum(data.count.capDevicesNum, 0, 1));
					$('#capDevicesWarn').text(
							formatNum(data.warn.capDevicesWarn, 0, 1));
					// cmt
					$('#cmtDevicesNum').text(
							formatNum(data.count.cmtDevicesNum, 0, 1));
					$('#cmtDevicesWarn').text(
							formatNum(data.warn.cmtDevicesWarn, 0, 1));
				}
			});
}

var dp_dataTableconfig = {

	"iDisplayLength" : 10,
	"processing" : true,
	"serverSide" : true,
	"paging" : true,
	"lengthChange" : false,
	"searching" : false,
	"ordering" : false,
	"info" : true,
	"autoWidth" : false,
	"scrollY" : 400,
	"oLanguage" : {
		"sProcessing" : "正在加载中......",
		// "sLengthMenu" : "每页显示 _MENU_ 条记录",
		"sZeroRecords" : "对不起，查询不到相关数据！",
		"sEmptyTable" : "暂无设备数据存在！",
		"sInfo" : "当前有共 _TOTAL_ 条设备记录",
		"sInfoFiltered" : "数据表中共为 _MAX_ 条记录",
		"sSearch" : "搜索",
		"oPaginate" : {
			// "sFirst" : "首页",
			"sPrevious" : "<<",
			"sNext" : ">>",
		// "sLast" : "末页"
		}
	},
	// d.device_id,d.code,d.is_use,d.name as plane_name,a.code as plane_code
	// ajax : "/device/loadDevices?1=1",
	columns : [
			{
				"data" : "plane_code"
			},
			{
				"data" : "flight_code",
				render : function(data, type, full) {
					if (data != null) {
						return data;
					} else {
						return '暂无关联航班';
					}
				}
			},
			{
				"data" : {
					"from_airport" : "from_airport",
					"to_airport" : "to_airport"
				},
				render : function(data, type, full) {
					if (data.from_airport != null) {
						return '<lable style="color:#0596ea">'
								+ data.from_airport + '-' + data.to_airport
								+ '</lable>';
					} else {
						return '-';
					}
				}

			},
			{
				"data" : {
					"fly_time" : "fly_time",
					"plan_land_time" : "plan_land_time"
				},
				render : function(data, type, full) {
					if (data.fly_time != null) {
						var flyTime = data.fly_time + "";
						var planLandTime = data.plan_land_time + "";
						return '<lable style="color:#">'
								+ flyTime.toDate().format('hh:mm tt') + '-'
								+ planLandTime.toDate().format('hh:mm tt')
								+ '</lable>';
					} else {
						return '-';
					}
				}

			},
			{
				"data" : "wifi_people",
				render : function(data, type, full) {
					if (data != null) {
						return data;
					} else {
						return '-';
					}
				}
			},
			{
				"data" : {
					"plane_id" : "plane_id",
					"plane_code" : "plane_code"
				},
				render : function(data, type, full) {
					if (data != null) {
						return '<lable style="color:#ffae00"><a onclick="PLANE_DEVICE.changeCharts('
								+ data.plane_id
								+ ',\''
								+ data.plane_code
								+ '\')" data-toggle="tab"><i class="fa fa-pie-chart"></i>概览</a>'
								+ ' | <a onclick="PLANE_DEVICE.showDetail('
								+ data.plane_id
								+ ',\''
								+ data.plane_code
								+ '\')" data-toggle="tab"><i class="fa fa-table"></i>详情</a></label>';
					} else {
						return '-';
					}
				}
			} ]
};

PLANE_DEVICE.loadPlanes = function(org_id) {
	if (!DP_PLANES_MAP.contain(org_id)) {
		var urlTmp = "/device/loadPlanes?1=1";
		var url = '&org_id=' + org_id;
		dp_dataTableconfig.ajax = urlTmp + url;
		$('#dp_plane_table_' + org_id).DataTable(dp_dataTableconfig);
		DP_PLANES_MAP.put(org_id, 0);
		$('#dp_plane_table_' + org_id).on(
				'xhr.dt',
				function(e, settings, json, xhr) {
					PLANE_DEVICE.initCharts(json.data[0].plane_id,
							json.data[0].plane_code);
				});
	}
}

PLANE_DEVICE.initCharts = function(planeId, planeCode) {
	// console.log('>>>>>>' + planeId);
	require.config({
		paths : {
			echarts : '//cdn.bootcss.com/echarts/2.2.7'
		}
	});
	require([ 'echarts', 'echarts/chart/line', 'echarts/chart/pie',
			'echarts/chart/bar' ], function(ec) {
		echarts = ec;
		DP_GL_KPI = DP_DEVICE_PAGE.createCircle('dp_device_kpi', planeId);
		DP_GL_FLOW = DP_DEVICE_FLOW.createFlowmon('dp_internet_flow', planeId);
		DP_GL_WARN = DP_DEVICE_WARN.createWarnmon('dp_device_warn', planeId);
		$(window).resize(function() {
			DP_GL_KPI.resize();
			DP_GL_FLOW.resize();
			DP_GL_WARN.resize();
		});
	});

	$('#dashbord_title').text('飞机' + planeCode + '指标概览信息');
}

PLANE_DEVICE.changeCharts = function(planeId, planeCode) {
	DP_DEVICE_PAGE.loadKPI(planeId);
	DP_DEVICE_FLOW.loadFlow(planeId);
	DP_DEVICE_WARN.loadWarn(planeId);
	$('#dashbord_title').text('飞机' + planeCode + '指标概览信息');
}

PLANE_DEVICE.showDetail = function(planeId, planeCode) {
	window.open('/device/showDetail?plane_id=' + planeId + '&plane_code='
			+ planeCode);
}

var orginBitMap = new Map();
var DPD_GL_DEVICE_MAP = new Map();
var DPD_GL_DEVICEID_MAP = new Map();
var DPD_GL_DEVICEIDS = [];
PLANE_DEVICE.createPLANE = function(planeId) {
	var canvas = document.getElementById('dpd_plane_canvas');
	var stage = new JTopo.Stage(canvas);
	var scene = new JTopo.Scene(stage);
	scene.background = '../../device/img/plane_inside.png';

	$.ajax({
		url : "/device/loadTopo",
		data : {
			"planeId" : planeId
		},
		success : function(data) {
			var containerWidth = 1388;// 原始比例分母
			// 原型定义新属性
			JTopo.Node.prototype.orginBit = null;
			JTopo.Node.prototype.resize = function() {
				var newX = ($(window).get(0).innerWidth - 50) * this.orginBit;
				this.setLocation(newX, this.y);
			};
			// topo节点
			var deviceList = data.dpd_device_topo;
			for (var i = 0; i < deviceList.length; i++) {
				var vo = deviceList[i];
				var x = parseInt(vo.x);
				var y = parseInt(vo.y);
				var width = parseInt(vo.width);
				var height = parseInt(vo.height);
				var url = vo.url;
				var type_code = vo.type_code;
				var node = createNode(x, y, width, height, url, type_code);

				scene.add(node);
				if (vo.url == null) {
					node.orginBit = node.x / containerWidth;
					node.resize();
				} else {
					node.borderColor = '0,150,0';
				}
				DPD_GL_DEVICE_MAP.put(vo.key_type, node);
				DPD_GL_DEVICEID_MAP.put(vo.device_id, node);

				if (vo.device_id != undefined && vo.device_id != null) {

					DPD_GL_DEVICEIDS.push(vo.device_id);
				}

			}

			// topo连线
			var linkList = data.dpd_device_link;
			for (var i = 0; i < linkList.length; i++) {
				var vo = linkList[i];
				var aNode = DPD_GL_DEVICE_MAP.get(vo.aNode);
				var zNode = DPD_GL_DEVICE_MAP.get(vo.zNode);
				var link = foldLink(aNode, zNode, '', 'vertical');
				scene.add(link);
			}

			// 比例定点缩放
			$(window).resize(function() {
				DPD_GL_DEVICE_MAP.each(function(key, value) {
					if (key.indexOf('-Z') == -1) {
						value.resize();
					}
				});
			});

			PLANE_DEVICE.initTopo();
		}
	});

}

PLANE_DEVICE.initTopo = function() {
	var tmp = '';
	for (var i = 0; i < DPD_GL_DEVICEIDS.length; i++) {
		tmp += DPD_GL_DEVICEIDS[i] + ',';
	}
	tmp = tmp.substring(0, tmp.length - 1);

	$.ajax({
		url : "/device/loadDeviceStatus",
		data : {
			"deviceIds" : tmp
		},
		success : function(data) {
			for (var i = 0; i < data.length; i++) {
				var node = DPD_GL_DEVICEID_MAP.get(data[i].device_id);
				var value = data[i].value;
				if (value == null) {
					node.alarm = '设备状态未知';
					node.borderColor = '255,0,0';
				} else if (value == '0') {
					node.alarm = '设备未启动';
					node.borderColor = '255,0,0';
				}

			}
		}
	});

	$.ajax({
		url : "/device/loadTopoWarn",
		data : {
			"deviceIds" : tmp
		},
		success : function(data) {
			for (var i = 0; i < data.length; i++) {
				var node = DPD_GL_DEVICEID_MAP.get(data[i].obj_id);
				node.alarm = '设备告警';
				node.borderColor = '255,0,0';
			}
		}
	});

}

// 机载cap
var dp_cap_datatable = {
	"iDisplayLength" : 10,
	"processing" : true,
	"serverSide" : true,
	"paging" : false,
	"lengthChange" : false,
	"searching" : false,
	"ordering" : false,
	"info" : false,
	"autoWidth" : false,
	"oLanguage" : {
		"sProcessing" : "正在加载中......",
		// "sLengthMenu" : "每页显示 _MENU_ 条记录",
		"sZeroRecords" : "对不起，查询不到相关数据！",
		"sEmptyTable" : "暂无设备数据存在！",
		"sInfo" : "当前有共 _TOTAL_ 条设备记录",
		"sInfoFiltered" : "数据表中共为 _MAX_ 条记录",
		"sSearch" : "搜索",
		"oPaginate" : {
			// "sFirst" : "首页",
			"sPrevious" : "<<",
			"sNext" : ">>",
		// "sLast" : "末页"
		}
	},
	columns : [
			{
				"data" : "cap_name"
			},
			{
				"data" : "cap_status",
				render : function(data, type, full) {
					if (data == '1') {
						return '<lable style="color:#005500">Online</label>';
					} else if (data == '0') {
						return '<lable style="color:#FF0000">Offline</label>';
					} else if (data == undefined) {
						return '<lable style="color:#ffae00">状态未知</label>';
					} else {
						return '<lable style="color:#ffae00">状态未知</label>';
					}
				}
			},
			{
				"data" : "cap_people",
				render : function(data, type, full) {
					if (data != null) {
						return data;
					} else if (data == null) {
						return '-';
					}

				}
			},
			{
				"data" : "cap_ip",
				render : function(data, type, full) {
					if (data != null) {
						return data;
					} else if (data == null) {
						return '-';
					}

				}
			},
			{
				"data" : "cap_mac",
				render : function(data, type, full) {
					if (data != null) {
						return data;
					} else if (data == null) {
						return '-';
					}

				}
			},
			{
				"data" : "cap_start_time",
				render : function(data, type, full) {
					if (data != null) {
						return data;
					} else if (data == null) {
						return '-';
					}

				}
			},
			{
				"data" : "device_id",
				render : function(data, type, full) {
					if (data != null) {
						return '<lable style="color:#ffae00"><a onclick="PLANE_DEVICE.showLog('
								+ data
								+ ')" data-toggle="tab"><i class="fa fa-table"></i>查看日志</a></label>';
					} else {
						return '-';
					}
				}
			} ]
};

PLANE_DEVICE.loadCap = function(planeId) {
	var urlTmp = "/device/loadCap?1=1";
	var url = '&planeId=' + planeId;
	dp_cap_datatable.ajax = urlTmp + url;
	$('#dpd_device_cap').DataTable(dp_cap_datatable);
}

// 飞机基本信息
PLANE_DEVICE.loadServerInfo = function(planeId, typeId) {

	$.ajax({
		url : "/device/loadServerInfo",
		async : true,
		data : {
			"planeId" : planeId,
			"typeId" : typeId,
		},
		success : function(data) {
			if (typeId == 104) {
				if (data['server.ip.mac'] != undefined) {
					$('#server_ip_mac').text(data['server.ip.mac']);
				}
				if (data['server.ip.ipv4'] != undefined) {
					$('#server_ip_ipv4').text(data['server.ip.ipv4']);
				}
				if (data['server.os.status'] != undefined) {
					var tmp = data['server.os.status'];
					if (tmp == '1') {
						tmp = '<lable style="color:#005500">Online</label>';
					} else if (data == '0') {
						tmp = '<lable style="color:#FF0000">Offline</label>';
					} else {
						tmp = '<lable style="color:#ffae00">' + tmp
								+ '</label>';
					}
					$('#server_os_status').html(tmp);
				}
				if (data['server.cpu.cpurate'] != undefined) {
					var tmp = data['server.cpu.cpurate'];
					$('#server_cpu_cpurate').text(tmp + '%');
				}
				if (data['server.memory.total'] != undefined) {
					var tmp = data['server.memory.total'];
					tmp = tmp / 1024 / 1024;
					$('#server_memory_total').text(Math.round(tmp) + 'G');
				}
				if (data['server.memory.free'] != undefined) {
					var tmp = data['server.memory.free'];
					tmp = tmp / 1024 / 1024;
					$('#server_memory_free').text(Math.round(tmp) + 'G');
				}
				if (data['server.CPUTMP.cputmp'] != undefined) {
					var tmp = data['server.CPUTMP.cputmp'];
					$('#server_CPUTMP_cputmp').text(tmp + '度');
				}
				if (data['server.CPUTMP.systmp'] != undefined) {
					var tmp = data['server.CPUTMP.systmp'];
					$('#server_CPUTMP_systmp').text(tmp + '度');
				}
			} else if (typeId == 106) {
				if (data['cmt.ip.mac'] != undefined) {
					$('#cmt_ip_mac').text(data['cmt.ip.mac']);
				}
				if (data['cmt.ip.ipv4'] != undefined) {
					$('#cmt_ip_ipv4').text(data['cmt.ip.ipv4']);
				}
				if (data['cmt.os.status'] != undefined) {
					var tmp = data['cmt.os.status'];
					if (tmp == '1') {
						tmp = '<lable style="color:#005500">Online</label>';
					} else if (data == '0') {
						tmp = '<lable style="color:#FF0000">Offline</label>';
					} else {
						tmp = '<lable style="color:#ffae00">' + tmp
								+ '</label>';
					}
					$('#cmt_os_status').html(tmp);
				}
				if (data['cmt.3G.3Gstatus'] != undefined) {
					var tmp = data['cmt.3G.3Gstatus'];
					if (tmp == '1') {
						tmp = '<lable style="color:#005500">Online</label>';
					} else if (data == '0') {
						tmp = '<lable style="color:#FF0000">Offline</label>';
					} else {
						tmp = '<lable style="color:#ffae00">' + tmp
								+ '</label>';
					}
					$('#cmt_3g_status').html(tmp);
				}
				if (data['cmt.3G.3GIP'] != undefined) {
					$('#cmt_3g_ip').text(data['cmt.3G.3GIP']);
				}
				if (data['cmt.cpu.cpurate'] != undefined) {
					var tmp = data['cmt.cpu.cpurate'];
					$('#cmt_cpu_cpurate').text(tmp + '%');
				}
				if (data['cmt.memory.total'] != undefined) {
					var tmp = data['cmt.memory.total'];
					tmp = tmp / 1024;
					$('#cmt_memory_total').text(Math.round(tmp) + 'M');
				}
				if (data['cmt.memory.free'] != undefined) {
					var tmp = data['cmt.memory.free'];
					tmp = tmp / 1024;
					$('#cmt_memory_free').text(Math.round(tmp) + 'M');
				}
				if (data['cmt.CPUTMP.cputmp'] != undefined) {
					var tmp = data['cmt.CPUTMP.cputmp'];
					$('#cmt_CPUTMP_cputmp').text(tmp + '度');
				}
				if (data['cmt.CPUTMP.systmp'] != undefined) {
					var tmp = data['cmt.CPUTMP.systmp'];
					$('#cmt_CPUTMP_systmp').text(tmp + '度');
				}
			}

		}
	});
}

PLANE_DEVICE.loadAppInfo = function(planeId, typeId) {
	var urlTmp = "/device/loadAppInfo?1=1";
	var url = '&planeId=' + planeId + "&typeId=" + typeId;
	dp_app_datatable.ajax = urlTmp + url;
	$('#dpd_device_app_' + typeId).DataTable(dp_app_datatable);
}

var dp_app_datatable = {
	"iDisplayLength" : 10,
	"processing" : true,
	"serverSide" : true,
	"paging" : false,
	"lengthChange" : false,
	"searching" : false,
	"ordering" : false,
	"info" : false,
	"autoWidth" : false,
	"oLanguage" : {
		"sProcessing" : "正在加载中......",
		// "sLengthMenu" : "每页显示 _MENU_ 条记录",
		"sZeroRecords" : "对不起，查询不到相关数据！",
		"sEmptyTable" : "暂无设备数据存在！",
		"sInfo" : "当前有共 _TOTAL_ 条设备记录",
		"sInfoFiltered" : "数据表中共为 _MAX_ 条记录",
		"sSearch" : "搜索",
		"oPaginate" : {
			// "sFirst" : "首页",
			"sPrevious" : "<<",
			"sNext" : ">>",
		// "sLast" : "末页"
		}
	},
	columns : [ {
		"data" : "app_name"
	}, {
		"data" : "app_runtime",
		render : function(data, type, full) {
			return secondToTime(data);
		}
	}, {
		"data" : "app_status",
		render : function(data, type, full) {
			if (data == '1') {
				return '<lable style="color:#005500">Running</label>';
			} else if (data == '0') {
				return '<lable style="color:#FF0000">Off</label>';
			} else {
				return '<lable style="color:#ffae00">' + data + '</label>';
			}
		}
	} ]
};

PLANE_DEVICE.loadDiskInfo = function(planeId, typeId) {
	var urlTmp = "/device/loadDisk?1=1";
	var url = '&planeId=' + planeId + "&typeId=" + typeId;
	dp_disk_datatable.ajax = urlTmp + url;
	$('#dpd_device_disk_' + typeId).DataTable(dp_disk_datatable);
}

var dp_disk_datatable = {
	"iDisplayLength" : 10,
	"processing" : true,
	"serverSide" : true,
	"paging" : false,
	"lengthChange" : false,
	"searching" : false,
	"ordering" : false,
	"info" : false,
	"autoWidth" : true,
	// "scrollX" : true,
	"oLanguage" : {
		"sProcessing" : "正在加载中......",
		// "sLengthMenu" : "每页显示 _MENU_ 条记录",
		"sZeroRecords" : "对不起，查询不到相关数据！",
		"sEmptyTable" : "<center>暂无设备数据存在！</center>",
		"sInfo" : "当前有共 _TOTAL_ 条设备记录",
		"sInfoFiltered" : "数据表中共为 _MAX_ 条记录",
		"sSearch" : "搜索",
		"oPaginate" : {
			// "sFirst" : "首页",
			"sPrevious" : "<<",
			"sNext" : ">>",
		// "sLast" : "末页"
		}
	},
	columns : [
			{
				"data" : "path"
			},
			{
				"data" : "rate",
				render : function(data, type, full) {
					data = data.substring(0, data.length - 1);
					var tmp = '<div class="progress margin-top-0 ">'
							+ '<div class="progress-bar progress-bar-aqua" style="width: '
							+ data + '%" role="progressbar" aria-valuenow="'
							+ data + '" aria-valuemin="0" aria-valuemax="100">'
							+ '<span >' + data + '%' + '</span>' + '</div>'
							+ '</div>';
					return tmp;
				}
			}, {
				"data" : {
					"used" : "used",
					"free" : "free"
				},
				render : function(data, type, full) {
					return '已使用:' + data.used + ',剩余:' + data.free;
				}
			} ]
};